#!/bin/bash

# Please be aware this script is used by cargo test also

TEST_MAC1="00:23:45:67:89:1a"
TEST_MAC2="00:23:45:67:89:1b"
TEST_MAC3="00:23:45:67:89:1c"
TEST_MAC4="00:23:45:67:89:1d"
TEST_MAC5="00:23:45:67:89:1f"
TEST_MAC_SIM0="00:23:45:67:89:20"
TEST_MAC_SIM1="00:23:45:67:89:21"

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0 1>/dev/null

if [ "CHK$1" == "CHK" ];then
    echo 'Need argument: bond, br, brv, vlan, dummy, vxlan, veth, vrf, sriov,'
    echo 'rm, route, rule, sim'
    exit 1
fi

LINK_WAIT_TIME=2

function clean_up {
    sudo ip link del veth1
    sudo ip link del mac0
    sudo ip link del macvtap0
    sudo ip link del br0
    sudo ip link del bond99
    sudo ip link del dummy1
    sudo ip link del vxlan0
    sudo ip link del eth1
    sudo ip link del eth2
    sudo ip link del vrf0
    sudo ip link del tun1
    sudo ip link del tun2
    sudo ip link del tap1
    sudo ip rule del priority 999
    sudo ip -6 rule del priority 999
    sudo modprobe -r netdevsim
}

function create_nics {
    clean_up 2>/dev/null
    sudo ip link add eth1 type veth peer name eth1.ep
    sudo ip link set eth1 address $TEST_MAC1
    sudo ip link add eth2 type veth peer name eth2.ep
    sudo ip link set eth2 address $TEST_MAC2
    sudo ip link set eth1.ep up
    sudo ip link set eth2.ep up
}

function create_netdevsim_nic {
    sudo modprobe netdevsim
    echo '1 2' | sudo tee /sys/bus/netdevsim/new_device
    sleep 5
    sudo ip link set eni1np1 down
    sudo ip link set eni1np1 name sim0
    sudo ip link set eni1np2 down
    sudo ip link set eni1np2 name sim1
    sudo ip link set sim0 address $TEST_MAC_SIM0
    sudo ip link set sim1 address $TEST_MAC_SIM1
}


if [ "CHK$1" == "CHKbond" ];then
    create_nics
    sudo ip link add bond99 type bond
    sudo ip link set eth1 master bond99
    sudo ip link set eth2 master bond99
    sudo ip link set eth1 up
    sudo ip link set eth2 up
    sudo ip link set bond99 address $TEST_MAC3
    sudo ip link set bond99 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKbr" ];then
    create_nics
    sudo ip link add br0 type bridge
    sudo ip link set eth1 master br0
    sudo ip link set eth2 master br0
    sudo ip link set eth1 up
    sudo ip link set eth2 up
    sudo ip link set br0 address $TEST_MAC3
    sudo ip link set br0 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKbrv" ];then
    create_nics
    sudo ip link add br0 type bridge
    sudo ip link set eth1 master br0
    sudo ip link set eth2 master br0
    sudo ip link set eth1 up
    sudo ip link set eth2 up
    sudo ip link set br0 address $TEST_MAC3
    sudo ip link set br0 up
    sudo bridge vlan add vid 10 pvid untagged dev eth1
    sudo bridge vlan add vid 2-4094 dev eth2
    sudo ip link set br0 type bridge vlan_filtering 1
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKvlan" ];then
    create_nics
    sudo modprobe 8021q
    sudo ip link set eth1 up
    sudo ip link set eth2 up
    sudo ip link add link eth1 name eth1.101 type vlan id 101
    sudo ip link set eth1.101 address $TEST_MAC3
    sudo ip link set eth1.101 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKdummy" ];then
    sudo ip link add dummy1 type dummy
    sudo ip link set dummy1 address $TEST_MAC1
    sudo ip link set dummy1 up
elif [ "CHK$1" == "CHKvxlan" ];then
    create_nics
    sudo ip link add vxlan0 type vxlan id 101 dstport 4789 dev eth1 \
        local 1.1.1.1 remote 8.8.8.8
    sudo ip link set eth1 up
    sudo ip link set vxlan0 address $TEST_MAC3
    sudo ip link set vxlan0 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKveth" ];then
    sudo ip link add veth1 type veth peer name veth1.ep
    sudo ip link set veth1 address $TEST_MAC1
    sudo ip link set veth1.ep address $TEST_MAC2
    sudo ip link set veth1 up
    sudo ip link set veth1.ep up
elif [ "CHK$1" == "CHKvrf" ];then
    create_nics
    sudo ip link add vrf0 type vrf table 10
    sudo ip link set vrf0 address $TEST_MAC3
    sudo ip link set vrf0 up
    sudo ip link set dev eth1 master vrf0
    sudo ip link set dev eth2 master vrf0
    sudo ip link set eth1 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKsriov" ];then
    create_netdevsim_nic
    echo 2 | sudo tee /sys/class/net/sim0/device/sriov_numvfs
    sudo ip link set sim0 vf 0 vlan 100 mac 36:f7:09:ef:95:f0
    sudo ip link set sim0 vf 1 mac 36:f7:09:ef:95:f1
    sudo ip link set sim0 up
elif [ "CHK$1" == "CHKtun" ];then
    sudo ip tuntap add name tun1 mode tun \
        user 1001 group 0 multi_queue vnet_hdr
    sudo ip link set tun1 up
    sudo ethtool -C tun1 rx-frames 60
    sudo ip tuntap add name tun2 mode tun \
        user 1001 group 0 multi_queue vnet_hdr
    sudo ip link set tun2 up
    sudo ethtool -C tun2 rx-frames 60
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKtap" ];then
    sudo ip tuntap add name tap1 mode tap \
        user 1001 group 0 multi_queue vnet_hdr
    sudo ip link set tap1 address $TEST_MAC3
    sudo ip link set tap1 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKmacvlan" ];then
    create_nics
    sudo ip link add mac0 link eth1 type macvlan mode source
    sudo ip link set link dev mac0 type macvlan macaddr add $TEST_MAC3
    sudo ip link set link dev mac0 type macvlan macaddr add $TEST_MAC4
    sudo ip link set mac0 address $TEST_MAC5
    sudo ip link set eth1 up
    sudo ip link set mac0 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKmacvtap" ];then
    create_nics
    sudo ip link add macvtap0 link eth1 type macvtap mode source
    sudo ip link set link dev macvtap0 type macvtap macaddr add $TEST_MAC2
    sudo ip link set link dev macvtap0 type macvtap macaddr add $TEST_MAC3
    sudo ip link set eth1 up
    sudo ip link set macvtap0 address $TEST_MAC5
    sudo ip link set macvtap0 up
    sleep $LINK_WAIT_TIME
elif [ "CHK$1" == "CHKroute" ];then
    create_nics
    sudo ip link set eth1 up
    sudo ip addr add 192.0.2.1/24 dev eth1
    sudo ip -6 addr add 2001:db8:f::1/64 dev eth1
    sudo ip route add 198.51.100.0/24 proto static scope global \
        nexthop via 192.0.2.254 dev eth1 weight 1 onlink \
        nexthop via 192.0.2.253 dev eth1 weight 256 onlink
    sudo ip -6 route add 2001:db8:e::/64 proto static scope global \
        nexthop via 2001:db8:f::254 dev eth1 weight 1 onlink \
        nexthop via 2001:db8:f::253 dev eth1 weight 256 onlink
elif [ "CHK$1" == "CHKrule" ];then
    create_nics
    sudo ip rule add from 192.0.2.1 to 192.0.2.2 \
        tos 10 table 100 iif eth1 oif eth2 priority 999
    sudo ip -6 rule add from 2001:db8:f::254 to 2001:db8:f::253 \
        tos 10 table 100 iif eth1 oif eth2 priority 999
elif [ "CHK$1" == "CHKsim" ];then
    create_netdevsim_nic
    sudo ethtool -A sim0 tx on
    sudo ethtool -A sim0 rx on
    sudo ethtool -A sim1 tx on
    sudo ethtool -A sim1 rx on
elif [ "CHK$1" == "CHKrm" ];then
    clean_up 2>/dev/null
fi
